﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DuplicateMP3Finder
{
    public partial class DuplicateMP3FinderMain : Form
    {
        protected List<string> corruptFiles = new List<string>();
        protected List<TagLib.File> duplicates;
        
        public DuplicateMP3FinderMain()
        {
            InitializeComponent();
        }

        private List<string> GetFolders(string directory, List<string> directories)
        { 
            if(!directories.Contains(directory))
            {
                directories.Add(directory);
            }
            foreach(string folder in Directory.GetDirectories(directory))
            {
                directories.Add(folder);
                GetFolders(folder, directories);
            }
            return directories;
        }

        private void submit_click(object sender, EventArgs e)
        {
            checkedListBox1.Items.Clear();
            checkedListBox1.Refresh();
            label2.Text = "";
            corruptFiles.Clear();
            
            Cursor = Cursors.WaitCursor;
            string[] files;
            List<string> folders = new List<string>();
            
            folders = GetFolders(textBox1.Text, folders);
            List<TagLib.File> mp3s = new List<TagLib.File>();


            foreach (string folder in folders)
            {
                files = Directory.GetFiles(folder, "*.m*");
                foreach (string file in files)
                {
                    try
                    {
                        TagLib.File f = TagLib.File.Create(file);
                        mp3s.Add(f);
                    }
                    catch (TagLib.CorruptFileException)
                    {
                        corruptFiles.Add(file);
                    }
                    catch (TagLib.UnsupportedFormatException)
                    {
                        
                    }
                }
            }

            label2.Text = System.String.Format("Found {0} songs in {1} folders and {2} corrupted files"
                , mp3s.Count, folders.Count, corruptFiles.Count);
            
            MP3Comparer mp3comparer = new MP3Comparer();
            duplicates = mp3comparer.findDuplicates(mp3s);

            
            foreach (TagLib.File file in duplicates)
            {
                string entry = System.String.Format("{0} | {1} | {2} | {3} | ",
                    file.Tag.Title != null ? file.Tag.Title : "Title Missing",
                    file.Tag.Performers != null ? file.Tag.Title : "Artist Missing",
                    file.Tag.Album != null ? file.Tag.Album : "Album Missing",
                    file.Name
                    );

                checkedListBox1.Items.Add(entry);
            }
            button3.Visible = true;
            button4.Visible = true;
            button5.Visible = true;
            button6.Visible = true;
            button7.Visible = true;
            Cursor = Cursors.Default;
        }

        private void browse_click(object sender, EventArgs e)
        {
            
            FolderBrowserDialog fd = new FolderBrowserDialog();
            fd.ShowDialog();
            textBox1.Text = fd.SelectedPath;

            
        }

        private void selDuplicates_click(object sender, EventArgs e)
        {
            string[] current;
            string prev = null;
            int last = 0;

            for(int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                current = checkedListBox1.Items[i].ToString().Split('|');
                
                if (prev == null)
                {
                    prev = current[0];
                    checkedListBox1.SetItemCheckState(i, CheckState.Checked);
                }
                else if (prev == current[0])
                    checkedListBox1.SetItemCheckState(last, CheckState.Checked);
                prev = current[0];
                last = i;
            }
        }

        private void delete_click(object sender, EventArgs e)
        {
            foreach(int i in checkedListBox1.CheckedIndices)
            {
                string[] file = checkedListBox1.Items[i].ToString().Split('|');
                try
                {
                    File.Delete(file[3]);
                }
                catch (IndexOutOfRangeException)
                {
                    File.Delete(file[0]);
                }
            }
            while (checkedListBox1.CheckedIndices.Count > 0)
            {
                int indice = -1;
                foreach (int i in checkedListBox1.CheckedIndices)
                {
                    indice = i;
                    break;
                }
                checkedListBox1.Items.RemoveAt(indice);
            }
            checkedListBox1.Refresh();
            
        }

        private void clear_click(object sender, EventArgs e)
        {
            foreach (int i in checkedListBox1.CheckedIndices)
            {
                checkedListBox1.SetItemCheckState(i, CheckState.Unchecked);
            }
        }

        private void viewCorrupted_click(object sender, EventArgs e)
        {
            checkedListBox1.Items.Clear();

            foreach (var file in corruptFiles)
            {
                checkedListBox1.Items.Add(file);
            }
            checkedListBox1.Refresh();
            button6.Visible = false;
            button8.Visible = true;
        }

        private void selectAll_click(object sender, EventArgs e)
        {
            for(int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                checkedListBox1.SetItemCheckState(i, CheckState.Checked);
            }
        }

        private void viewDuplicates_Click(object sender, EventArgs e)
        {
            checkedListBox1.Items.Clear();
            foreach (TagLib.File file in duplicates)
            {
                string entry = System.String.Format("{0} | {1} | {2} | {3} | ",
                    file.Tag.Title != null ? file.Tag.Title : "Title Missing",
                    file.Tag.Performers != null ? file.Tag.Title : "Artist Missing",
                    file.Tag.Album != null ? file.Tag.Album : "Album Missing",
                    file.Name
                    );

                checkedListBox1.Items.Add(entry);
            }
            checkedListBox1.Refresh();
            button8.Visible = false;
            button6.Visible = true;
        }
    }
}
